import { getTokenKey } from "../../utils/auth"
const app = getApp();

Component({
  properties: {
    imgList: {
      type: Array,
      default:[]
    }
  },

  data: {
    urlList:[],
    imgMaxNumber: 4,
  },

  /**
   * 组件的方法列表
   */
  methods: { 

    ChooseImage() {
      var that = this
      wx.chooseMedia({
        count: this.data.imgMaxNumber, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        mediaType: ['image','video'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          if (that.data.imgList.length != 0) {
            that.setData({
              imgList: that.data.imgList.concat(res.tempFiles),
            })
            that.triggerEvent('chooseEvent', that.data.imgList)
          } else {
              that.setData({
                imgList: res.tempFiles,
              })
              that.triggerEvent('chooseEvent', that.data.imgList)
          }
        }
      }); 
    },

    ViewImage(e) {
      var imgList = this.data.imgList
      console.log(imgList)
      var urlList = []
      imgList.forEach(item => {
        if(item.img){
          urlList.push(item.img)
        }else if(item.tempFilePath){
          urlList.push(item.tempFilePath)
        }
      })
      wx.previewImage({
        urls: urlList,
        current: e.currentTarget.dataset.url
      });
    },
  
    DelImg(e) {
      wx.showModal({
        title: '确定删除这张图片吗？',
        cancelText: '再看看',
        confirmText: '确定',
        success: res => {
          if (res.confirm) {
            this.data.imgList.splice(e.currentTarget.dataset.index, 1);
            this.setData({
              imgList: this.data.imgList
            })
            this.triggerEvent('chooseEvent', this.data.imgList)
          }
        }
      })
    }
  }
})
